/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package unc.pds.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import unc.pds.data.Consts;
import unc.pds.model.CommentRemote;
import unc.pds.model.Commentable;
import unc.pds.model.DiaryRemote;
import unc.pds.model.Model;
import unc.pds.model.UserRemote;
import unc.pds.service.ServiceBeanRemote;
import unc.pds.service.ServiceBeanRemoteHome;
import unc.pds.service.UserHolder;
import unc.pds.util.Arch;
import unc.pds.util.ArchImpl;

/**
 *
 * @author Михаил
 */
public class CommentServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        try {
            ServiceBeanRemote service = ((ServiceBeanRemoteHome)(new InitialContext()).lookup("ejb/ServiceBean")).create((Long)request.getSession().getAttribute("sid"));
            if (request.getParameter("id") != null) {
                CommentRemote comment = (CommentRemote) service.getByKey(new Long(Long.parseLong(request.getParameter("id"))));
                comment.setCaption(request.getParameter("title"));
                comment.setComment(request.getParameter("content"));
                response.sendRedirect("index.jsp?id=" + comment.getKey());
            } else {
                Arch doc = new ArchImpl("root", null);
                Commentable post = (Commentable) service.getByKey(new Long(Long.parseLong(request.getParameter("postid"))));
                doc = new ArchImpl("root", null);
                doc.addChild(new ArchImpl(Consts.ATTR__COMMENT_CAPTION, request.getParameter("title")));
                doc.addChild(new ArchImpl(Consts.CONT__COMMENT_POST, request.getParameter("content")));
                doc.addChild(new ArchImpl(Consts.ATTR__COMMENT_AUTHOR, service.getUser().getKey().toString()));
                post.addComment(doc);
                while (post instanceof CommentRemote) {
                    post = (Commentable)post.getParent();
                }
                response.sendRedirect("index.jsp?id=" + post.getKey());
            }
        } catch (Exception e) {
            out.println(e.getLocalizedMessage());
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
